package com.arica.framework.mq.consumer.config;

import com.arica.mq.consumer.listener.BaseConsumerBean;
import com.arica.mq.consumer.listener.BaseMQConsumerProperties;
import lombok.Data;
import org.apache.rocketmq.common.protocol.heartbeat.MessageModel;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Data
@Component
@ConfigurationProperties(prefix = "rocketmq")
public class CustomConsumerProperties {

    private static final String MESSAGE_MODEL = "CLUSTERING";

    private String nameSrvAddr;
    private String env;
    private List<String> topicList = new ArrayList<>();
    @Value("${rocketmq.consumer.group}")
    private String group;
    @Value("${rocketmq.consumer.consumeThreadNums}")
    private Integer consumeThreadNums;
    @Value("${rocketmq.consumer.maxReconsumeTimes}")
    private Integer maxReconsumeTimes;
    @Value("${rocketmq.consumer.messageModel}")
    private String messageModel;

    private BaseMQConsumerProperties baseMQConsumerProperties;

    @Bean
    public BaseMQConsumerProperties baseMQConsumerProperties() {
        BaseMQConsumerProperties baseMQConsumerProperties = new BaseMQConsumerProperties() {
            @Override
            public boolean isOnOff() {
                return Boolean.TRUE;
            }
        };

        baseMQConsumerProperties.setNameSrvAddr(nameSrvAddr);
        baseMQConsumerProperties.setGroup(group);
        baseMQConsumerProperties.setEnv(env);
        baseMQConsumerProperties.setConsumeThreadNums(consumeThreadNums);
        baseMQConsumerProperties.setMaxReconsumeTimes(maxReconsumeTimes);
        baseMQConsumerProperties.setTopicList(topicList);
        baseMQConsumerProperties.setMessageModel(MESSAGE_MODEL.equals(messageModel) ? MessageModel.CLUSTERING : MessageModel.BROADCASTING);

        this.baseMQConsumerProperties = baseMQConsumerProperties;
        return baseMQConsumerProperties;
    }

    @Bean
    public BaseConsumerBean baseConsumerBean() {
        BaseConsumerBean baseConsumerBean = new BaseConsumerBean();
        baseConsumerBean.setCommonProperties(baseMQConsumerProperties);
        return baseConsumerBean;
    }

}
